/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simhya.matheval.function;

import simhya.matheval.SymbolArray;
import simhya.matheval.Expression;

/**
 *
 * @author Luca
 */
public class DynamicFunction extends FunctionDefinition {
    SymbolArray arguments;
    Expression definition;

    public DynamicFunction(String name, int arity, Expression definition) {
        super.name = name;
        super.arity = arity;
        super.minimumArity = 0;
        super.type = FunctionType.DYNAMIC;
        super.randomFunction = definition.isRandom();
        this.definition = definition;
        super.isLogical = definition.isLogicalExpression();
    }

    
    @Override
    public double compute() {
        throw new UnsupportedOperationException("This is a dynamic function. Call compute(double[] args)");
    }

    @Override
    public double compute(double x) {
        throw new UnsupportedOperationException("This is a dynamic function. Call compute(double[] args)");
    }

    @Override
    public double compute(double x1, double x2) {
        throw new UnsupportedOperationException("This is a dynamic function. Call compute(double[] args)");
    }

    @Override
    public double compute(double[] args) {
        arguments = SymbolArray.getNewFastEvalSymbolArray(arity);
        for (int i=0;i<arity;i++)
            arguments.setValue(i, args[i]);
        return definition.computeValue(arguments);
    }

}
